<?php
namespace app\index\controller;
use think\View;
// use \think\Config;
use think\Db;
use app\index\model\Books212002125;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
         return "这是王昊的Tp5图书管理系统";
    }

         
    //显示图书列表
    public function lists(){  
        $this->assign([
                'book'=>Books212002125::getBookList()
        ]);

               return $this->fetch('lists',[],[
                '__CSS__'=>'/static/css'
               ]);
         
    }


    //添加数据

    public function add(){
            return $this->fetch();
        }

    public function addBook()
        {
            $data = \think\Request::instance()->post();
             $link = Db::connect();
            if ($data['publish_time'] == '') {
                $this->error('删除失败，正在跳转','add','',1);
                return;
            }
             $result = $link->table('books212002125')->insert([
                    'name' => $data['name'], 
                    'category' =>$data['category'],
                    'price' =>$data['price'],
                    'publish_time' => $data['publish_time']
             ]);
            if ($result) {
                $this->success('添加成功，正在跳转','lists','',1);
            } else {
                $this->error('添加失败，正在跳转','add','',1);
            }
        }


   
    
    //更新数据
       public function edit($id){
         $books = new Books212002125();
        $data = $books::get($id);
        $this->assign('data', $data);
        return $this->fetch();
    }
            

        public function update(){
            $link = Db::connect();
            $data = \think\Request::instance()->post();
             $result = $link->table('books212002125')->update([
                    'id' => $data['id'], 
                    'name' => $data['name'], 
                    'category' =>$data['category'],
                    'price' =>$data['price'],
                    'publish_time' => $data['publish_time']
                    ]);
        if ($result) {
           $this->success('更新成功，正在跳转','lists','',1);;
        } else {
           $this->error('更新失败，正在跳转','lists','',1);
        }

        }


    //删除数据
          public function delete1($id){
             $requset = \think\Request::instance();
             $result = Db::table('books212002125')
                    ->where(['id'=>$id])
                    ->delete();
             if($result) 
             {
              $this->success('删除成功，正在跳转','lists','',1);
              } else{
                 $this->error('删除失败，正在跳转','lists','',1);
                 }
          }

   


}








?>